package com.itheima.day3.stream;

import java.util.*;
import java.util.stream.Stream;


public class C11CollectTest {
    record Hero(String name, int strength) {

    }

    /*
        收集：将元素收集入容器
            .collect(() -> c, (c, x) -> void, ?)

            () -> c             创建容器 c
            (c, x) -> void      将元素 x 加入 容器 c
     */
    public static void main(String[] args) {
        Stream<String> stream = Stream.of("令狐冲", "风清扬", "独孤求败", "方证",
                "东方不败", "冲虚", "向问天", "任我行", "不戒", "不戒", "不戒", "不戒");

        // ------------------收集到集合容器---------------
        // 1) 收集到 List
        List<String> result = stream.collect(() -> new ArrayList<>(), (list, x) -> list.add(x), (a, b) -> {});
        // ArrayList::new   () -> new ArrayList()
        // ArrayList::add   (list, x) -> list.add(x)
        List<String> result2 = stream.collect(ArrayList::new, ArrayList::add, (a, b) -> {});

        // 2) 收集到 Set
        Set<String> result3 = stream.collect(LinkedHashSet::new, Set::add, (a, b) -> {});

        // 3)收集到 Map (key、value)
        Map<String, Integer> result4 = stream.collect(HashMap::new, (map, x) -> map.put(x, 1), (a, b) -> {});
        for (Map.Entry<String, Integer> entry : result4.entrySet()) {
            System.out.println(entry);
        }

        // ---------------收集到字符串容器-----------------
        // 4) 收集到StringBuilder
        StringBuilder sb = stream.collect(StringBuilder::new, StringBuilder::append, (a, b) -> {});
        System.out.println(sb);

        // 5) 收集到StringJoiner 添加分隔符
        StringJoiner sj = stream.collect(() -> new StringJoiner(","), StringJoiner::add, (a, b) -> {});
        System.out.println(sj);
    }
}
